package com.dofast.bt.account;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;

import com.dofast.bt.base.BaseApplication;
import com.dofast.bt.util.SpfUtil;
import com.google.gson.Gson;

/**
 * Created by Administrator on 2015/8/3.
 */
public class AccountManager  {

    private static AccountManager mInstance;
    private SharedPreferences mSP;
    private Account mActive;
    private Handler mHandler;
    private Context mContext;
    public String source;


    private static final String REQUEST_HEAD_AUTHORIZATION = "authorization";

    public static final String SP_KEY_ACTIVE_ID = "activeUserId";
    public static final int NOTIFY_TYPE_LOGOUT = 1;
    public static final int NOTIFY_TYPE_LOGIN = NOTIFY_TYPE_LOGOUT + 1;
    public static final int NOTIFY_TYPE_UPDATE = NOTIFY_TYPE_LOGIN + 1;
    private int notifyTypeLogout;

    private AccountManager() {}

    public static AccountManager getInstance() {
        if (mInstance == null) {
            synchronized (AccountManager.class) {
                if (mInstance == null) {
                    mInstance = new AccountManager(BaseApplication.getInstance());
                }
            }
        }
        return mInstance;
    }

    private AccountManager(Context context) {
        mSP = context.getSharedPreferences("accounts", 0);
        mHandler = new Handler(Looper.getMainLooper());
        mContext = context.getApplicationContext();

//        String activeId = getActiveUserId();
//        if (!TextUtils.isEmpty(activeId)) {
//            mActive = getAccount(activeId);
//        }
    }


    /**
     * 登录
     *
     */
    public void login(Account account) {

        if (account == null ) {
            return;
        }

        // 原有帐号退出
        if (mActive != null && !mActive.equals(account)) {
//            logout(mActive, false);
            mActive = null;
        }

//        if (mActive != null) {
//            // 用更新的避免chatEvent为空
//            mActive.update(account);
//        } else {
//            mActive = account;
//        }
//        notifyObserver(account, NOTIFY_TYPE_LOGIN);

//        // 更新用户资料
        SpfUtil.getInstance().put(SP_KEY_ACTIVE_ID, account.getToken());
        SpfUtil.getInstance().put(account.getToken(),  new Gson().toJson(account));
    }

    private void logout(Account account, boolean clearPicassoCache) {
        if(account == null || TextUtils.isEmpty(account.getToken())) {
            return;
        }
        try {
//            PushManager.stopWork(mContext);
        }catch (SecurityException e){};
        SpfUtil.getInstance().remove(account.getToken());
        SpfUtil.getInstance().remove(SP_KEY_ACTIVE_ID);
        mActive = null;
        System.gc();
    }
    /**
     * 帐号退出
     *
     * @param account
     */
    public void logout(Account account) {
        logout(account, true);
    }


    public Account getAccount(String id) {
        Account result = null;
        if (!TextUtils.isEmpty(id)) {
            if (mActive != null && mActive.getToken().equals(id)) {
                result = mActive;
            } else {
//                String local = mSP.getString(id, null);
                String local = SpfUtil.getInstance().get(id);
                if (!TextUtils.isEmpty(local)) {
                    result = new Gson()
                            .fromJson(local, Account.class);
                }
            }
        }
        return result;
    }


}
